package com.ticket.api.controller;

import com.ticket.api.service.TbUserInfoService;
import com.ticket.common.string.StringUtil;
import com.ticket.common.time.CommonFunction;
import com.ticket.db.entity.TblUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("User")
public class TbUserInfoController {
    private static Logger log = LoggerFactory.getLogger(TbUserInfoController.class);

    @Autowired
    TbUserInfoService userInfoService;

    @RequestMapping("/login")
    public TblUserInfo findTblUserInfoByCondition(@RequestParam  String openid,TblUserInfo userInfo) {
        try {
            if(StringUtil.isEmpty(userInfo.getOpenId())){ userInfo.setOpenId(openid); }
            if (StringUtil.isEmpty(openid)){ return null; }
            TblUserInfo info=  userInfoService.findTblUserInfoByCondition(openid);
            if(info==null){
                boolean fig =  userInfoService.insertTblUserInfo(userInfo);
                if(fig==true){
                    TblUserInfo user = userInfoService.findTblUserInfoByCondition(openid);
                    user.setLastUpdateTime(CommonFunction.Toyymmdd(user.getLastUpdateTime()));
                    return user;
                }
            }
            System.out.println(info);
            return  info;
        }catch (Exception e){
            log.error("login"+e);
        }
        return  null;
    }
    @RequestMapping("/updateUser")
    public Integer updateTblUserInfo(TblUserInfo info) {
        try{
            return userInfoService.updateTblUserInfo(info);
        }catch (Exception e){
            log.error("updateUser"+e);
            return null;
        }

    }
}
